using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class PatronBooksCheckedOut : System.Web.UI.UserControl
{
    ManageCheckedOutBook bookInfo;
    CheckedOutBook book;
    Patron patron;
    Library lib;
    ManageLibrary mlib;
    string libID;
    DateTime dueDate;
    DaysUntil.DaysUntilDue booksDueIn;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            bookInfo = new ManageCheckedOutBook();
            book = new CheckedOutBook();
            mlib = new ManageLibrary();
            lib = new Library();
            patron = (Patron)Session["Patron"];
            libID = Request.QueryString["id"].ToString();
            lib = mlib.getLibrary(libID);
            booksDueIn = new DaysUntil.DaysUntilDue();
            dueDate = new DateTime();
            lblDaysUntilDue.Visible = false;
            getBooksCheckedOut();
        }
        catch (Exception ex)
        {
            lblError.Text = "error: " + ex.ToString();
        }

    }

    private void getBooksCheckedOut()
    {
        SqlDataAdapter data = bookInfo.getCheckedOutBooks(patron.PersonID);
        DataSet d = new DataSet();
        data.Fill(d);

        gvBooksCheckedOut.DataSource = d;
        gvBooksCheckedOut.DataBind();

        if (gvBooksCheckedOut.Rows.Count < 1)
        {
            lblMessage.Text  = "You have no books checked out.";
            gvBooksCheckedOut.Visible = false;
            lblDaysUntilDue.Visible = false;
        }
        else
        {
            getDueDate();
            lblMessage.Text = "";
            gvBooksCheckedOut.Visible = true;
        }
    }

    private bool isBookRenewable(string barcode)
    {
        if (bookInfo.isBookRenewable(barcode) == 0)
            return true;
        else
            return false;
    }

    private void renewBook()
    {
        try
        {

            if (!isBookRenewable(gvBooksCheckedOut.SelectedDataKey.Value.ToString()))
            {
                lblError.Text = "This book is not renewable.";
                lblSuccess.Visible = false;
                lblError.Visible = true;
            }
            else
            {
                int length = lib.RenewLength;
                book = bookInfo.checkedOutBookDetails(gvBooksCheckedOut.SelectedDataKey.Value.ToString());
                DateTime newDueDate = bookInfo.getDate().AddDays(length);
                book.dueDate = newDueDate;
                bookInfo.updateCheckedOutBook(book);
                lblSuccess.Text = "You have renewed the due date for this book.";
                lblError.Visible = false;
                lblSuccess.Visible = true;
            }
        }
        catch (Exception ex)
        {
            lblError.Text = "error: " + ex.ToString();
        }

    }

    protected void gvBooksCheckedOut_SelectedIndexChanged1(object sender, EventArgs e)
    {
        renewBook();
        getBooksCheckedOut();
    }

    private void getDueDate()
    {
        dueDate = bookInfo.getDueDate(patron.PersonID);
        int days = booksDueIn.DaysUntil(dueDate);
        lblDaysUntilDue.Visible = true;
        if (days < 0)
        {
            int due = Math.Abs(days);
            lblDaysUntilDue.Text = "You have books that are " + due + " days overdue. Please return them to the library.";
            lblDaysUntilDue.ForeColor = System.Drawing.Color.Red;
        }
        else if (days > 0)
        {
            lblDaysUntilDue.Text = "You have books due in " + days + " days";
        }
        else
        {
            lblDaysUntilDue.Text = "You have books due today.";
            lblDaysUntilDue.ForeColor = System.Drawing.Color.Red;
        }
        //lblDaysUntilDue.Text = days.ToString();
    }
}
